<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改图片</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
    <script src="${ctxPath}/static/manage/js/jquery.js"></script>
    <script src="${ctxPath}/static/manage/layui/layui.js"></script>
    <script src="${ctxPath}/static/manage/js/x-layui.js" charset="utf-8"></script>
</head>
<body>
<form class="layui-form" id="formUpdateImg" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="x-red">*</span>电脑图片</label>
        <div class="layui-col-md3 layui-col-xs5">
            <div class="layui-upload-list thumbBox mag0 magt3">
                <img class="layui-upload-img thumbImg">
            </div>
        </div>
        <input type="hidden" id="id" value="${computerDetails.id}" name="id">

        <input type="hidden" id="picture" name="picture" value="${computerDetails.picture}">

    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <button class="layui-btn" type="button" lay-filter="add" lay-submit="">增加</button>
    </div>
</form>
</body>
</html>
<script type="text/javascript">
    layui.use(['form', 'layer', 'upload'], function () {
        var $ = layui.$, form = layui.form, upload = layui.upload, layer = layui.layer;

        // 上传缩略图
        upload.render({
            elem: '.thumbBox',
            size: 600,
            url: '/kaptcha/fileUpload',
            data:{
                id:$("#id").val()
            },
            before: function (obj) {
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                layer.load();
                obj.preview(function (index, file, result) {
                    $('.thumbImg').attr('src', result);
                });
            },
            done: function (res) {
                // alert("图片上传成功吗，保存后生效");
                layer.closeAll('loading');
                $("#picture").val(res.data);
                $('.thumbBox').css("background", "#fff");
                if (res.code != 0) {
                    alert(res.data);
                }}

            });

        //监听提交
        form.on('submit(add)', function () {
            var picture=$("#picture").val();
            if(picture==""){
                alert("请选择图片");
                return;
            }

            $.ajax({
                url: '/manage/computerDetails/computerDetailsUpdateImg',
                type: 'post',
                data:$("#formUpdateImg").serialize(),
                success: function (result) {
                    alert(result);
                    window.parent.location.reload();
                },
                error: function (result) {
                    alert("失败？请及时告知开发人员" + result);
                    window.parent.location.reload();
                }

            });
        })
    });
</script>